﻿using System.Windows.Forms;
using UIShell.OSGi.Utility;

namespace Pasys.PresentationCore.WinForms
{
    public class ShellLayoutViewProxy : IShellLayoutViewProxy
    {
        public string SessionKey { get; private set; }
        private readonly Action<IPersistViewProvider> _showSmartPart;

        public ShellLayoutViewProxy(Control owner, Action<IPersistViewProvider> showSmartPart)
        {
            SessionKey = System.Guid.NewGuid().ToString();
            AssertUtility.ArgumentNotNull(owner, "owner");
            Owner = owner;
            _showSmartPart = showSmartPart;
        }

        #region IShellLayoutView 成员

        public void RemoveMenuItem(ToolStripItem control)
        {
            Invoke(() => MainMenuStrip.Items.Remove(control));
        }

        public void RemoveToolStrip(ToolStrip control)
        {
            Invoke(() =>
                       {
                           if (control.Parent != null)
                           {
                               control.Parent.Controls.Remove(control);
                           }
                       });
        }
        public void InsertMenuItem(int index,ToolStripItem control)
        {
            Invoke(() => MainMenuStrip.Items.Insert(index,control));
        }

        public void AddMenuItem(ToolStripItem control)
        {
            Invoke(() => MainMenuStrip.Items.Add(control));
        }

        public void AddToolStrip(ToolStrip control)
        {
            //The first row is reserved for main menu.
            Invoke(() => ToolStripContainer.TopToolStripPanel.Join(control, 1));
        }

        public void SetStatusLabel(string text)
        {
            Invoke(() => StatusLabel.Text = text);
        }

        public void Invoke(Action action)
        {
            if (Owner.InvokeRequired)
            {
                Owner.BeginInvoke(new MethodInvoker(action));
            }
            else
            {
                action();
            }
        }

        #endregion

        #region IShellLayoutView 成员

        //public void Show(object smartPart, object smartPartInfo)
        //{
        //    Invoke(() => _showSmartPart(smartPart, smartPartInfo));
        //}

        //public void Show(object smartPart)
        //{
        //    Show(smartPart, null);
        //}

        public void Show(IPersistViewProvider viewProvider)
        {
            Invoke(() => _showSmartPart(viewProvider));           
        }

        #endregion

        public ILayoutViewProxyContainer FindOwnerForm()
        {
            return Owner.FindForm() as ILayoutViewProxyContainer;
        }

        public Control Owner { get; private set; }
        public ToolStripContainer ToolStripContainer { get; set; }
        public StatusStrip MainStatusStrip { get; set; }
        public MenuStrip MainMenuStrip { get; set; }
        public ToolStripStatusLabel StatusLabel { get; set; }
    }
}